const Koa = require("koa");
const Router = require("@koa/router");

const app = new Koa();
const router = new Router();

router.get("/", (ctx) => {
  ctx.body = "Hello Koa";
});

router.get("/foo", (ctx) => {
  ctx.body = "Foo page";
});

app.use(router.routes()).use(router.allowedMethods());

// koa 没有路由系统，只有中间件
/* app.use((ctx) => {
  const path = ctx.path;
  if (path === "/") {
    ctx.body = "home page";
  } else if (path === "/foo") {
    ctx.body = "foo page";
  } else {
    ctx.body = "404 Not Found";
  }
}); */

app.listen(3000, () => {
  console.log("Server is running at http://localhost:3000");
});
